package com.key.web_admin.controller;

import com.key.common.base.RespResult;
import com.key.modules.dto.CarrierAddDTO;
import com.key.modules.dto.CarrierEditDTO;
import com.key.modules.service.CarrierService;
import com.key.modules.vo.CarrierDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wei
 */
@RestController
@RequestMapping("/carrier")
public class CarrierController {

    @Autowired
    CarrierService carrierService;

    /**
     * 查询承运商表（支持条件查询）
     * @author wei
     * @param currentPage 当前页
     * @param pageSize 页的大小
     * @param cName 承运商名称（条件查询时的可选参数）
     * @param cType 类型（条件查询时的可选参数）
     * @param cRegisteredTime 注册时间（条件查询时的可选参数）
     * @return
     */
    @GetMapping("/findCarrierList")
    public RespResult findCarrierList(Integer currentPage, Integer pageSize,
                                      String cName, String cType, String cRegisteredTime) {
        RespResult respResult;
        try{
            respResult = carrierService.findCarrierList(currentPage, pageSize, cName, cType, cRegisteredTime);
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }

        return respResult;
    }

    /**
     * 查询承运商详情
     * @author wei
     * @param cId 承运商ID
     * @return
     */
    @GetMapping("/findCarrierDetails")
    public RespResult findCarrierDetails(Integer cId) {
        RespResult respResult;
        try{
            CarrierDetailsVO carrierDetailsVO = carrierService.findCarrierDetails(cId);
            respResult = RespResult.success(carrierDetailsVO);
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 承运商新增
     * @author wei
     * @param carrierAddDTO 新增承运商时需要填写的参数
     * @return
     */
    @PostMapping("/addCarrier")
    public RespResult addCarrier(@RequestBody CarrierAddDTO carrierAddDTO) {
        RespResult respResult;
        try{
            boolean result = carrierService.addCarrier(carrierAddDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 承运商删除
     * @author wei
     * @param cId 承运商ID
     * @return
     */
    @PostMapping("/removeCarrier")
    public RespResult removeCarrier(Integer cId) {
        RespResult respResult;
        try{
            boolean result = carrierService.removeCarrier(cId);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 承运商修改
     * @author wei
     * @param carrierEditDTO 修改承运商时需要填写的参数
     * @return
     */
    @PostMapping("/editCarrier")
    public RespResult editCarrier(@RequestBody CarrierEditDTO carrierEditDTO) {
        RespResult respResult;
        try{
            boolean result = carrierService.editCarrier(carrierEditDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

}
